package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.service.MarketKeywordService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class MarketKeywordServiceImpl implements MarketKeywordService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Override
    public List<MarketKeyword> readKeywordList(String page, String limit, String sort, String order, String keyword, String url) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.orderBy(sort + " " + order);
        MarketKeywordExample.Criteria criteria = marketKeywordExample.or().andDeletedEqualTo(false);
        if (keyword!=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (url!=null && !"".equals(url)){
            criteria.andUrlLike("%"+url+"%");
        }
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        return marketKeywords;
    }
    @Transactional
    @Override
    public List<MarketKeyword> InsertKeyword(String keyword, String url, String isHot, String isDefault) {

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        boolean isHotBool = "true".equals(isHot);
        marketKeyword.setIsHot(isHotBool);
        boolean isDefaultBool = "true".equals(isDefault);
        marketKeyword.setIsDefault(isDefaultBool);
//        marketKeyword.setId(null);
//       marketKeyword.setDeleted(true);
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setSortOrder(100);
        marketKeyword.setDeleted(false);
        int insert = marketKeywordMapper.insert(marketKeyword);
        System.out.println("insert:"+insert);
        List<MarketKeyword> marketKeywords = new ArrayList<>();
        if (insert>0){
            marketKeywords.add(marketKeyword);
            return marketKeywords;
        }
        return marketKeywords;
    }
    @Transactional
    @Override
    public List<MarketKeyword> updateKeyword(String id, String keyword, String url, String isHot, String isDefault, String addTime, String updateTime) {
        
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setId(Integer.parseInt(id));
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        boolean isHotBool = "true".equals(isHot);
        marketKeyword.setIsHot(isHotBool);
        boolean isDefaultBool = "true".equals(isDefault);
        marketKeyword.setIsDefault(isDefaultBool);
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setSortOrder(100);
        marketKeyword.setDeleted(false);
        int i = marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        System.out.println("update:"+i);
        List<MarketKeyword> marketKeywords = new ArrayList<>();
        if (i>0){
            marketKeywords.add(marketKeyword);
            return marketKeywords;
        }
        return marketKeywords;

    }
    @Transactional
    @Override
    public List<MarketKeyword> deleteKeyword(String id, String keyword, String url, String isHot, String isDefault, String addTime, String updateTime) {
//        SqlSession session = MyBatisUtil.getSession();
//        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
//        MarketKeyword marketKeyword = new MarketKeyword();
//        int i = mapper.deleteByPrimaryKey(Integer.parseInt(id));
//        System.out.println("delete:"+i);
//        session.commit();
//        session.close();
//        List<MarketKeyword> marketKeywords = new ArrayList<>();
//        if (i>0){
//            marketKeywords.add(marketKeyword);
//            return marketKeywords;
//        }
//        return marketKeywords;

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setId(Integer.parseInt(id));
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        boolean isHotBool = "true".equals(isHot);
        marketKeyword.setIsHot(isHotBool);
        boolean isDefaultBool = "true".equals(isDefault);
        marketKeyword.setIsDefault(isDefaultBool);
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setSortOrder(100);
        marketKeyword.setDeleted(true);
        System.out.println("here");
        int i = marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        System.out.println("update:"+i);
        List<MarketKeyword> marketKeywords = new ArrayList<>();
        if (i>0){
            marketKeywords.add(marketKeyword);
            return marketKeywords;
        }
        return marketKeywords;
    }
}
